<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <?php include 'inc/head.php'; ?>
</head>

<body>
  <?php include 'inc/header.php'; ?>

  <div class="container">
    <div class="btn-group btn-group-sm">
      <a href="<?php echo M('admin', 'database', 'index') ?>" class="btn btn-primary">返回</a>
      <a href="<?php echo M('admin', 'database', 'backup') ?>" class="btn btn-primary">备份</a>
      <a href="javascript:void(0);" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modal-prompt">还原</a>
    </div>
    <hr>
    <table class="table table-sm table-bordered table-hover">
      <thead class="bg-light">
        <tr>
          <th>序号</th>
          <th>名称</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <?php
        if (is_dir($this->dir)) {
          $file = scandir($this->dir);

          if (isset($file[2])) {
            foreach ($file as $val) {
              if ($val != '.' && $val != '..') {
                $arr[] = $val;
              }
            }

            rsort($arr);

            foreach ($arr as $key => $val) {
              $file = $dir . $val;
              echo '<tr><td>' . ($key + 1) . '</td><td><a href="/sql/' . $file . '">' . $val . '</a></td><td><a href="' . M('admin', 'database', 'del') . '&f=' . $file . '" onclick="return confirm(\'确认要删除吗？\')" >删除</a></td></tr>';
            }
          } else {
            echo '<tr><td colspan=3 align=center>暂时没有备份数据</td></tr>';
          }
        } else {
          echo '<tr><td colspan=3 align=center>备份文件夹不存在</td></tr>';
        }
        ?>
      </tbody>
    </table>

  </div>

  <?php include 'inc/footer.php'; ?>

  <div class="modal fade" tabindex="-1" id="modal-prompt">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">请输入需还原的文件名</div>
        <div class="modal-body">
          <input type="text" class="form-control ipt-restore" placeholder="每次还原一个库文件，文件名请参考下表">
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-light btn-restore">提交</button>
          <button type="button" class="btn btn-light" data-bs-dismiss="modal" aria-label="Close">取消</button>
        </div>
      </div>
    </div>
  </div>

  <script>
  $(function() {
    $('.btn-restore').click(function() {
      var ipt = $(".ipt-restore");
      if (ipt.val() == '') {
        alert('数据库名称不能为空');
        return false;
      }
      window.location.href = '<?php echo M('admin', 'database', 'restore'); ?>&db=' + ipt.val();
    });
    $(".ipt-restore").on("input propertychange", function() {
      var val = $(this).val();
      val = val.replace(/\t/g, "");
      $(this).val(val);
    });
  });
  </script>

</body>

</html>